
const Constant = require("./constant.js");
const WaveFcfaPageTag = require("./waveFcfaPageTag.js");
const RunDefine = require("./baseRunDefine.js");
const WaveFcfaPageUtil = require("./waveFcfaPageUtil.js");
const momoXafPageUtil = require("./momoXafPageUtil.js");
const SmsUtil = require("./baseSmsUtil.js");


// 操作 登录
const doLogin = () => {

    // 找到 0520
    let btn0 = id("com.wave.personal:id/zero").findOnce();
    let btn5 = id("com.wave.personal:id/num5").findOnce();
    let btn2 = id("com.wave.personal:id/num2").findOnce();

    // console.log(Constant.UI_SDK_VERSION + "查找 0: " + btn0.text());
    // console.log(Constant.UI_SDK_VERSION + "查找 5: " + btn5.text());
    // console.log(Constant.UI_SDK_VERSION + "查找 2: " + btn2.text());

    sleep(1 * 1000);
    if (btn0 && btn5 && btn2) {
        console.log(Constant.UI_SDK_VERSION + "登录: 点击 0: ");
        btn0.click();
        sleep(1 * 800);

        console.log(Constant.UI_SDK_VERSION + "登录: 点击 5");
        btn5.click();
        sleep(1 * 800);

        console.log(Constant.UI_SDK_VERSION + "登录: 点击 2");
        btn2.click();
        sleep(1 * 800);

        console.log(Constant.UI_SDK_VERSION + "登录: 点击 0");
        btn0.click();
        sleep(1 * 800);

        return true;
    }

    console.log(Constant.UI_SDK_VERSION + "操作登录结束: 当前不是登录页")
    return false;
};

// ==================== 流水操作-start ====================
// 上报缓存记录
const mBillIdsCached = [];

// 首屏记录
const mBillFirstScreenCached = [];

// 测试
const testArray1 = [];

// 测试
const doTestArray = (index) => {

    let bill1 = [{
        shortType: 'Sent to Qin L 07 10 00 24 59',
        shortDate: 'Mar 07, 10:56',
        shortAmount: '-10F'
    },
    {
        shortType: 'Sent to Qin L 07 10 00 24 59',
        shortDate: 'Mar 07, 10:45',
        shortAmount: '-10F'
    },
    {
        shortType: 'Sent to Qin L 07 10 00 24 59',
        shortDate: 'Mar 07, 10:27',
        shortAmount: '-10F'
    },
    {
        shortType: 'Sent to Qin L 07 10 00 24 59',
        shortDate: 'Mar 07, 10:22',
        shortAmount: '-10F'
    },
    {
        shortType: 'Sent to Qin L 07 10 00 24 59',
        shortDate: 'Mar 06, 18:45',
        shortAmount: '-15F'
    },
    {
        shortType: 'Sent to Qin L 07 10 00 24 59',
        shortDate: 'Mar 06, 18:24',
        shortAmount: '-15F'
    }];

    let bill2 = [{
        shortType: 'Sent to Qin L 07 10 00 24 59',
        shortDate: 'Mar 07, 10:45',
        shortAmount: '-10F'
    },
    {
        shortType: 'Sent to Qin L 07 10 00 24 59',
        shortDate: 'Mar 07, 10:27',
        shortAmount: '-10F'
    },
    {
        shortType: 'Sent to Qin L 07 10 00 24 59',
        shortDate: 'Mar 07, 10:22',
        shortAmount: '-10F'
    },
    {
        shortType: 'Sent to Qin L 07 10 00 24 59',
        shortDate: 'Mar 06, 18:45',
        shortAmount: '-15F'
    },
    {
        shortType: 'Sent to Qin L 07 10 00 24 59',
        shortDate: 'Mar 06, 18:24',
        shortAmount: '-15F'
    },
    {
        shortType: 'Sent to Qin L 07 10 00 24 59',
        shortDate: 'Mar 06, 18:16',
        shortAmount: '-15F'
    }];

    let chagedStatus = doIsBillsListChanged(bill1, bill2);
    console.log(chagedStatus);

    // let list = [];
    // for (let i = 0; i < index; i++) {
    //     let content = (i + 1) + "";
    //     let item = {};
    //     item.shortType = content;
    //     item.shortDate = content;
    //     item.shortAmount = content;
    //     list.push(item);
    // }
    // doDeepCloneShortBills(testArray1, list);

    // console.log("======")
    // console.log(testArray1);
    // console.log("======")
}

// 深拷贝首页短流水记录
const doDeepCloneShortBills = (arr1, arr2) => {
    if (!arr2) {
        return;
    }

    if (!arr1) {
        arr1 = [];
        return;
    }

    // 重置
    arr1.length = 0;
    // 深拷贝
    for (let di = 0; di < arr2.length; di++) {
        let diItem = arr2[di];
        let cloneItem = {};
        cloneItem.shortType = diItem.shortType;
        cloneItem.shortDate = diItem.shortDate;
        cloneItem.shortAmount = diItem.shortAmount;
        arr1.push(cloneItem);
    }
};

// 判断两个bill item是否相同
const doIsSameBillItems = (billItem1, billItem2) => {
    if (!billItem1) {
        return false;
    }
    if (!billItem2) {
        return false;
    }

    // 判断是否发生变化
    let isAmountChanged = (billItem1.shortAmount !== billItem2.shortAmount);
    let isDateChanged = (billItem1.shortDate !== billItem2.shortDate);
    let isTypeChanged = (billItem1.shortType !== billItem2.shortType);
    if (isAmountChanged || isDateChanged || isTypeChanged) {
        return false;
    }

    // 默认相同
    return true;
}

// 判断两屏数据是否发生变动
const doIsBillsListChanged = (bill1, bill2) => {
    // 改变状态详情
    let changedStatus = {
        isBillsChanged: false,
        isNeedCheckAll: false,
        recheckBills: []
    };
    if ((!bill1) && (bill2)) {
        changedStatus.isBillsChanged = false;
        changedStatus.isNeedCheckAll = false;
        changedStatus.recheckBills.length = 0;
        return changedStatus;
    }
    if ((bill1) && (!bill2)) {
        changedStatus.isBillsChanged = true;
        changedStatus.isNeedCheckAll = true;
        changedStatus.recheckBills.length = 0;
        return changedStatus;
    }
    if ((bill1.length > 0) && (bill2.length == 0)) {
        changedStatus.isBillsChanged = true;
        changedStatus.isNeedCheckAll = true;
        changedStatus.recheckBills.length = 0;
        return changedStatus;
    }
    if ((bill1.length == 0) && (bill2.length > 0)) {
        changedStatus.isBillsChanged = false;
        changedStatus.isNeedCheckAll = false;
        changedStatus.recheckBills.length = 0;
        return changedStatus;
    }

    // 长度 <
    if (bill1.length < bill2.length) {
        let bill2Item0 = bill2[0];
        changedStatus.isBillsChanged = true;
        changedStatus.isNeedCheckAll = false;
        changedStatus.recheckBills.length = 0;

        // 循环遍历
        for (let rIndex = 0; rIndex < bill1.length; rIndex++) {
            let bill1ItemNext = bill1[rIndex];
            let billItemChangedNext = (!doIsSameBillItems(bill1ItemNext, bill2Item0));
            console.log("======= " + rIndex + "-billItemChangedNext:" + billItemChangedNext);

            // 第n个发生变化
            if (billItemChangedNext) {
                let changeItemNext = {};
                changeItemNext.shortAmount = bill1ItemNext.shortAmount;
                changeItemNext.shortDate = bill1ItemNext.shortDate;
                changeItemNext.shortType = bill1ItemNext.shortType;
                changedStatus.recheckBills.push(changeItemNext);
            } else {
                break;
            }
        }

        return changedStatus;
    }

    // 长度 =
    if (bill1.length === bill2.length) {
        let bill1Item0 = bill1[0];
        let bill2Item0 = bill2[0];
        // 判断是否发生变化
        let billItem0Changed = (!doIsSameBillItems(bill1Item0, bill2Item0));

        // 第1个发生变化
        if (billItem0Changed) {
            // 添加缓存
            changedStatus.isBillsChanged = true;
            changedStatus.isNeedCheckAll = false;
            changedStatus.recheckBills.length = 0;
            let changeItem0 = {};
            changeItem0.shortAmount = bill1Item0.shortAmount;
            changeItem0.shortDate = bill1Item0.shortDate;
            changeItem0.shortType = bill1Item0.shortType;
            changedStatus.recheckBills.push(changeItem0);

            // 循环遍历
            for (let rIndex = 1; rIndex < bill1.length; rIndex++) {
                let bill1ItemNext = bill1[rIndex];
                let billItemChangedNext = (!doIsSameBillItems(bill1ItemNext, bill2Item0));
                console.log("======= " + rIndex + "-billItemChangedNext:" + billItemChangedNext);

                // 第n个发生变化
                if (billItemChangedNext) {
                    let changeItemNext = {};
                    changeItemNext.shortAmount = bill1ItemNext.shortAmount;
                    changeItemNext.shortDate = bill1ItemNext.shortDate;
                    changeItemNext.shortType = bill1ItemNext.shortType;
                    changedStatus.recheckBills.push(changeItemNext);
                } else {
                    break;
                }
            }
            return changedStatus;
        }
    }

    // 长度 >
    if (bill1.length > bill2.length) {
        let bill2Item0 = bill2[0];
        changedStatus.isBillsChanged = true;
        changedStatus.isNeedCheckAll = false;
        changedStatus.recheckBills.length = 0;

        // 循环遍历
        for (let rIndex = 0; rIndex < bill1.length; rIndex++) {
            let bill1ItemNext = bill1[rIndex];
            let billItemChangedNext = (!doIsSameBillItems(bill1ItemNext, bill2Item0));
            console.log("======= " + rIndex + "-billItemChangedNext:" + billItemChangedNext);

            // 第n个发生变化
            if (billItemChangedNext) {
                let changeItemNext = {};
                changeItemNext.shortAmount = bill1ItemNext.shortAmount;
                changeItemNext.shortDate = bill1ItemNext.shortDate;
                changeItemNext.shortType = bill1ItemNext.shortType;
                changedStatus.recheckBills.push(changeItemNext);
            } else {
                break;
            }
        }

        return changedStatus;
    }

    // for (let i = 0; i < bill1.length; i++) {
    //     let item1 = bill1[i];
    //     let item2 = bill2[i];

    //     let isAmountChanged = (item1.shortAmount !== item2.shortAmount);
    //     let isDateChanged = (item1.shortDate !== item2.shortDate);
    //     let isTypeChanged = (item1.shortType !== item2.shortType);
    //     if (isAmountChanged || isDateChanged || isTypeChanged) {
    //         changedStatus.isBillsChanged = true;
    //         // 找到哪个不一样

    //         return changedStatus;
    //     }
    // }

    changedStatus.isBillsChanged = false;
    changedStatus.isNeedCheckAll = false;
    changedStatus.recheckBills.length = 0;
    return changedStatus;
};

// 新增流水详情缓存
const doAddBillRecord = (billItem) => {
    if (billItem) {
        let transactionID = billItem.detailTransactionID;
        if (doIsBillRecordExist(transactionID)) {
            return false;
        } else {
            mBillIdsCached.push(billItem);
            return true;
        }
    }
    return false;
};

// 查询交易流水是否存在缓存
const doIsBillRecordExist = (transactionID) => {
    if (!transactionID) {
        return false;
    }

    for (let i = 0; i < mBillIdsCached.length; i++) {
        let item = mBillIdsCached[i];
        if (item.detailTransactionID === transactionID) {
            console.log(Constant.UI_SDK_VERSION + "doIsBillRecordExist-交易id已存在, 不作处理-" + transactionID);
            return true;
        }
    }
    console.log(Constant.UI_SDK_VERSION + "doIsBillRecordExist-交易id不存在, 增加到缓存-" + transactionID);

    return false;
};

// 点击当前需要查看详情的行
const doCheckBillDetailForSpecialBills = (bills) => {
    if (!bills) {
        return true;
    }
    if (bills.length === 0) {
        return true;
    }
    // 如果数据有变化, 读取当前列表
    let textList = className("TextView").find();
    let startIndex = 0;
    while (startIndex < textList.length) {
        let curTv = textList[startIndex];
        let curText = curTv.text() || "";
        console.log(Constant.UI_SDK_VERSION + "doCheckBillDetailForSpecialBills-当前: " + startIndex + ", 内容: " + curText);

        // 判断是否为有效的记录
        let isRecordTagSent = (curText.indexOf(WaveFcfaPageTag.billRecordSent1) !== -1);
        let isRecordTagReceived = (curText.indexOf(WaveFcfaPageTag.billRecordReceived1) !== -1);
        if (isRecordTagSent || isRecordTagReceived) {
            // 这是行开头, 再判断是否为完整的行记录
            let amountIndex = (startIndex + 1);
            let dateIndex = (startIndex + 2);

            if ((amountIndex < textList.length) && (dateIndex < textList.length)) {
                // 只有包含金额与日期的数据, 才是完整的行记录数据
                let amountTv = textList[amountIndex];
                let dateTv = textList[dateIndex];

                let amountText = amountTv.text() || "";
                let dateText = dateTv.text() || "";

                // 判定日期字段是否为日期格式
                if (momoXafPageUtil.isMonthScaleWords(dateText)) {
                    // 判断是否可以点击
                    let canClick = (dateTv.bounds().top < (device.height - 0));
                    console.log(Constant.UI_SDK_VERSION + "doCheckBillDetailForSpecialBills-日期top: " + dateTv.bounds().top + ", canClick: " + canClick);

                    if (!canClick) {
                        // 最后一行的Y坐标
                        lastRowY = dateTv.bounds().top - 0;
                        startIndex = textList.length;

                        // 不是有效的行数据, 需要往下滑
                        console.log(Constant.UI_SDK_VERSION + "doCheckBillDetailForSpecialBills-已经读完当前屏幕的有效数据, 跳出循环,  最后一行的Y坐标: " + lastRowY);
                        continue;
                    }

                    // 判断是否可以点击
                    let isCanBeClickItem = false;
                    for (let bIndex = 0; bIndex < bills.length; bIndex++) {
                        let bItem = bills[bIndex];
                        let isSameAmount = (bItem.shortAmount === amountText);
                        let isSameDate = (bItem.shortDate === dateText);
                        let isSameType = (bItem.shortType === curText);
                        if (isSameAmount && isSameDate && isSameType) {
                            isCanBeClickItem = true;
                            break;
                        }
                    }

                    // 如果可点击，查看详情
                    if (isCanBeClickItem) {
                        // 添加缓存
                        let rowData = {};
                        rowData.shortType = curText;
                        rowData.shortDate = dateText;
                        rowData.shortAmount = amountText;

                        // 查看详情
                        console.log(Constant.UI_SDK_VERSION + "doCheckBillDetailForSpecialBills-点击查看详情: " + curText + " , 并原地轮询10秒");
                        click(curText);
                        sleep(1000);

                        // 循环判断
                        // 判断是否为 账单详情页
                        for (let billDetailIndex = 0; billDetailIndex < 10; billDetailIndex++) {
                            // 计算公式： (n+1)%10 = 0 时上报短信
                            if (((billDetailIndex + 1) % 10) === 0) {
                                // 上传短信
                                SmsUtil.syncSmsData();
                            }

                            // 确认进入了 账单详情页
                            if (WaveFcfaPageUtil.isBillDetailPage()) {
                                // 开始获取数据
                                let transactionId = doGetBillDetails(rowData);
                                console.log(Constant.UI_SDK_VERSION + "doCheckBillDetailForSpecialBills-流水号 " + transactionId);
                                // 清空缓存
                                rowData = null;

                                sleep(1000);

                                // 判断 android.widget.ProgressBar
                                for (let i = 0; i < RunDefine.loopQueryCount; i++) {
                                    let refreshBar = className("android.widget.ProgressBar").findOnce();
                                    if (refreshBar != null) {
                                        console.log(Constant.UI_SDK_VERSION + "doScanBillsForEachScreen-列表刷新中");
                                        sleep(1000);
                                        continue;
                                    } else {
                                        console.log(Constant.UI_SDK_VERSION + "doScanBillsForEachScreen-没找到刷新标志");
                                        break;
                                    }
                                }
                                break;
                            }
                            sleep(1 * 1000);
                        }
                    }

                    console.log(Constant.UI_SDK_VERSION + "doCheckBillDetailForSpecialBills-准备下一个游标=====");
                    // 更新起始位置
                    startIndex = (dateIndex + 1);
                    // sleep(1 * 1000);
                    continue;

                } else {
                    // 不是有效的行数据, 需要往下滑
                    console.log(Constant.UI_SDK_VERSION + "doCheckBillDetailForSpecialBills-当前: " + startIndex + ", 不是有效数据行==最后不是以日期行结束");
                }

            } else {
                // 不是有效的行数据, 需要往下滑
                console.log(Constant.UI_SDK_VERSION + "doCheckBillDetailForSpecialBills-当前: " + startIndex + ", 不是有效数据行==只展示部分行");
            }
        } else {
            // 不是有效的行数据, 需要继续遍历
            console.log(Constant.UI_SDK_VERSION + "doCheckBillDetailForSpecialBills-当前: " + curText + ", 不是有效数据行==不是Sent与Receive开头");
        }
        startIndex += 1;
    }

    // 释放内存
    textList.length = 0;
    textList = null;

    return true;
};

// 查询当前屏的数据
const doGetBillsForCurrentScreen = () => {
    // 是否滑动到底部
    let hadSwipeToBottom = false;

    // 数据列表
    let dataList = [];

    // 读取当前列表
    let textList = className("TextView").find();
    let startIndex = 0;
    while (startIndex < textList.length) {
        let curTv = textList[startIndex];
        let curText = curTv.text() || "";
        // console.log(Constant.UI_SDK_VERSION + "doGetBillsForCurrentScreen-当前: " + startIndex + ", 内容: " + curText);

        // 判断是否到了底部
        if (!hadSwipeToBottom) {
            hadSwipeToBottom = (curText.indexOf(WaveFcfaPageTag.homePageTag5) !== -1);
        }

        // 判断是否为有效的记录
        let isRecordTagSent = (curText.indexOf(WaveFcfaPageTag.billRecordSent1) !== -1);
        let isRecordTagReceived = (curText.indexOf(WaveFcfaPageTag.billRecordReceived1) !== -1);
        if (isRecordTagSent || isRecordTagReceived) {
            // 这是行开头, 再判断是否为完整的行记录
            let amountIndex = (startIndex + 1);
            let dateIndex = (startIndex + 2);

            if ((amountIndex < textList.length) && (dateIndex < textList.length)) {
                // 只有包含金额与日期的数据, 才是完整的行记录数据
                let amountTv = textList[amountIndex];
                let dateTv = textList[dateIndex];

                let amountText = amountTv.text() || "";
                let dateText = dateTv.text() || "";

                // 判定日期字段是否为日期格式
                if (momoXafPageUtil.isMonthScaleWords(dateText)) {
                    // 判断是否可以点击
                    let canClick = (dateTv.bounds().top < (device.height - 0));
                    // console.log(Constant.UI_SDK_VERSION + "doGetBillsForCurrentScreen-日期top: " + dateTv.bounds().top + ", canClick: " + canClick);

                    if (!canClick) {
                        // 最后一行的Y坐标
                        lastRowY = dateTv.bounds().top - 0;
                        startIndex = textList.length;

                        // 不是有效的行数据, 需要往下滑
                        // console.log(Constant.UI_SDK_VERSION + "doGetBillsForCurrentScreen-已经读完当前屏幕的有效数据, 跳出循环,  最后一行的Y坐标: " + lastRowY);
                        continue;
                    }

                    // 添加缓存
                    let rowData = {};
                    rowData.shortType = curText;
                    rowData.shortDate = dateText;
                    rowData.shortAmount = amountText;
                    dataList.push(rowData);

                    // console.log(Constant.UI_SDK_VERSION + "doGetBillsForCurrentScreen-准备下一个游标=====");
                    // 更新起始位置
                    startIndex = (dateIndex + 1);
                    // sleep(1 * 1000);
                    continue;
                }
            }
        }
        startIndex += 1;
    }

    textList.length = 0;
    textList = null;

    // 打印
    console.log(Constant.UI_SDK_VERSION + "doGetBillsForCurrentScreen-共有效数据: " + dataList.length);
    // for (let i = 0; i < dataList.length; i++) {
    //     let item = dataList[i];
    //     console.log(Constant.UI_SDK_VERSION + "doGetBillsForCurrentScreen-" + i + ": " + item.shortType);
    // }

    return { list: dataList, foundBottom: hadSwipeToBottom };
};

// 处理每个版页的数据需求
const doScanBillsForEachScreen = () => {
    // 是否为一个周期
    let isOneLoop = true;

    // 控制缓存不超过15条
    let needDelCount = (mBillIdsCached.length - 15);
    if (needDelCount > 0) {
        mBillIdsCached.splice(0, needDelCount);
    }

    // 是否滑动到底部
    let hadSwipeToBottom = false;

    // 最多遍历5次, 测试来看足够了
    let xPullStart = (device.width * 0.8);
    let yPullStart = 280;
    let yPullEnd = (device.height - 60);

    // 滑动到顶部
    swipe(xPullStart, yPullStart, xPullStart, yPullEnd, 1000);
    sleep(2 * 1000);

    // 下拉刷新下
    swipe(xPullStart, 400, xPullStart, 800, 1000);
    sleep(3 * 1000);

    for (let pageSize = 0; pageSize < 10; pageSize++) {
        // 是否已找出第行有效记录的坐标
        let lastRowY = -1;

        // 是否还在首屏, 即包含三词： Send Payments Bank
        let hadFoundSend = false;
        let hadFoundPayments = false;
        let hadFoundBank = false;

        // 先查出当前屏的有效数据
        let curScreenBillListData = doGetBillsForCurrentScreen();
        let curScreenBillList = curScreenBillListData.list;
        hadSwipeToBottom = curScreenBillListData.foundBottom;

        // 判断首屏标记
        let isFirstScreen = (pageSize === 0);

        // 如果两屏数据一样, 不作点击
        if (isOneLoop) {
            isOneLoop = false;
            console.log("======== " + pageSize + "-curScreenBillList-" + curScreenBillList.length);
            console.log(curScreenBillList);
            console.log("======== " + pageSize + "-mBillFirstScreenCached-" + mBillFirstScreenCached.length);
            console.log(mBillFirstScreenCached);
            let chagedStatus = doIsBillsListChanged(curScreenBillList, mBillFirstScreenCached);
            let isChagedForLastBillList = chagedStatus.isBillsChanged;
            console.log("======== " + pageSize + "-chagedStatus-isChanged-" + isChagedForLastBillList);
            console.log(chagedStatus.recheckBills);

            if (chagedStatus.isBillsChanged) {
                if (!chagedStatus.isNeedCheckAll) {
                    // 直接点击详情查看
                    doCheckBillDetailForSpecialBills(chagedStatus.recheckBills);
                    console.log(Constant.UI_SDK_VERSION + "doScanBillsForEachScreen-已处理完变化的数据, 退出循环");
                    return true;
                }
            } else {
                if (isFirstScreen) {
                    console.log(Constant.UI_SDK_VERSION + "doScanBillsForEachScreen-当前屏数据与上一屏数据没变化, 不作处理");
                    // 上报当前这版的数据
                    // ...
                    return true;
                }
            }

            // let isChagedForLastBillList = doIsBillsListChanged(curScreenBillList, mBillFirstScreenCached).isBillsChanged;
            // console.log("======== " + pageSize + "-isChanged: " + isChagedForLastBillList);

            // if (isFirstScreen && (!isChagedForLastBillList)) {
            //     console.log(Constant.UI_SDK_VERSION + "doScanBillsForEachScreen-当前屏数据与上一屏数据没变化, 不作处理");
            //     // 上报当前这版的数据
            //     // ...
            //     return true;
            // }
        }

        // 测试
        sleep(1 * 1000);

        // 保存首屏数据
        if (isFirstScreen) {
            console.log(Constant.UI_SDK_VERSION + "doScanBillsForEachScreen-是首屏数据, 先保存");
            doDeepCloneShortBills(mBillFirstScreenCached, curScreenBillList);
        }

        // 如果数据有变化, 读取当前列表
        let textList = className("TextView").find();
        let startIndex = 0;
        while (startIndex < textList.length) {
            let curTv = textList[startIndex];
            let curText = curTv.text() || "";
            console.log(Constant.UI_SDK_VERSION + "doScanBillsForEachScreen-当前: " + startIndex + ", 内容: " + curText);

            // 判断是否在首屏
            if (isFirstScreen) {
                if (!hadFoundSend) {
                    hadFoundSend = (curText.indexOf(WaveFcfaPageTag.homePageTag2) !== -1);
                }
                if (!hadFoundPayments) {
                    hadFoundPayments = (curText.indexOf(WaveFcfaPageTag.homePageTag3) !== -1);
                }
                if (!hadFoundBank) {
                    hadFoundBank = (curText.indexOf(WaveFcfaPageTag.homePageTag4) !== -1);
                }
            }

            // 判断是否到了底部
            if (!hadSwipeToBottom) {
                hadSwipeToBottom = (curText.indexOf(WaveFcfaPageTag.homePageTag5) !== -1);
            }
            console.log(Constant.UI_SDK_VERSION + "doScanBillsForEachScreen-当前-是否在首屏: " + (hadFoundSend || hadFoundPayments || hadFoundBank) + ", 是否到底: " + hadSwipeToBottom)

            // 判断是否为有效的记录
            let isRecordTagSent = (curText.indexOf(WaveFcfaPageTag.billRecordSent1) !== -1);
            let isRecordTagReceived = (curText.indexOf(WaveFcfaPageTag.billRecordReceived1) !== -1);
            if (isRecordTagSent || isRecordTagReceived) {
                // 这是行开头, 再判断是否为完整的行记录
                let amountIndex = (startIndex + 1);
                let dateIndex = (startIndex + 2);

                if ((amountIndex < textList.length) && (dateIndex < textList.length)) {
                    // 只有包含金额与日期的数据, 才是完整的行记录数据
                    let amountTv = textList[amountIndex];
                    let dateTv = textList[dateIndex];

                    let amountText = amountTv.text() || "";
                    let dateText = dateTv.text() || "";

                    // 判定日期字段是否为日期格式
                    if (momoXafPageUtil.isMonthScaleWords(dateText)) {
                        // 判断是否可以点击
                        let canClick = (dateTv.bounds().top < (device.height - 0));
                        console.log(Constant.UI_SDK_VERSION + "doScanBillsForEachScreen-日期top: " + dateTv.bounds().top + ", canClick: " + canClick);

                        if (!canClick) {
                            // 最后一行的Y坐标
                            lastRowY = dateTv.bounds().top - 0;
                            startIndex = textList.length;

                            // 不是有效的行数据, 需要往下滑
                            console.log(Constant.UI_SDK_VERSION + "doScanBillsForEachScreen-已经读完当前屏幕的有效数据, 跳出循环,  最后一行的Y坐标: " + lastRowY);
                            continue;
                        }

                        // 添加缓存
                        let rowData = {};
                        rowData.shortType = curText;
                        rowData.shortDate = dateText;
                        rowData.shortAmount = amountText;

                        // 查看详情
                        console.log(Constant.UI_SDK_VERSION + "doScanBillsForEachScreen-点击查看详情: " + curText + " , 并原地轮询10秒");
                        click(curText);
                        sleep(1000);

                        // 循环判断
                        // 判断是否为 账单详情页
                        for (let billDetailIndex = 0; billDetailIndex < 10; billDetailIndex++) {
                            // 计算公式： (n+1)%10 = 0 时上报短信
                            if (((billDetailIndex + 1) % 10) === 0) {
                                // 上传短信
                                SmsUtil.syncSmsData();
                            }

                            // 确认进入了 账单详情页
                            if (WaveFcfaPageUtil.isBillDetailPage()) {
                                // 开始获取数据
                                let transactionId = doGetBillDetails(rowData);
                                console.log(Constant.UI_SDK_VERSION + "doScanBillsForEachScreen-流水号 " + transactionId);
                                // 清空缓存
                                rowData = null;

                                sleep(1000);

                                // 判断 android.widget.ProgressBar
                                for (let i = 0; i < RunDefine.loopQueryCount; i++) {
                                    let refreshBar = className("android.widget.ProgressBar").findOnce();
                                    if (refreshBar != null) {
                                        console.log(Constant.UI_SDK_VERSION + "doScanBillsForEachScreen-列表刷新中");
                                        sleep(1000);
                                        continue;
                                    } else {
                                        console.log(Constant.UI_SDK_VERSION + "doScanBillsForEachScreen-没找到刷新标志");
                                        break;
                                    }
                                }
                                break;
                            }
                            sleep(1 * 1000);
                        }

                        // 再次查询当前屏的数据
                        let recheckScreenBillList = doGetBillsForCurrentScreen().list;
                        let isBillsChanged = doIsBillsListChanged(curScreenBillList, recheckScreenBillList).isBillsChanged;
                        console.log(Constant.UI_SDK_VERSION + "doScanBillsForEachScreen-屏幕数据是否变动: " + isBillsChanged);

                        if (isBillsChanged) {
                            console.log(Constant.UI_SDK_VERSION + "doScanBillsForEachScreen-屏幕数据有变动, 需要重置");
                            // 重置
                            curScreenBillList.length = 0;
                            curScreenBillList = [];
                            // 深拷贝
                            doDeepCloneShortBills(curScreenBillList, recheckScreenBillList);

                            textList.length = 0;
                            textList = null;
                            textList = className("TextView").find();
                            startIndex = 0;

                            recheckScreenBillList.length = 0;
                            recheckScreenBillList = null;

                            // sleep(1 * 1000);
                            continue;
                        } else {
                            recheckScreenBillList.length = 0;
                            recheckScreenBillList = null;
                        }

                        console.log(Constant.UI_SDK_VERSION + "doScanBillsForEachScreen-准备下一个游标=====");
                        // 更新起始位置
                        startIndex = (dateIndex + 1);
                        // sleep(1 * 1000);
                        continue;

                    } else {
                        // 不是有效的行数据, 需要往下滑
                        console.log(Constant.UI_SDK_VERSION + "doScanBillsForEachScreen-当前: " + startIndex + ", 不是有效数据行==最后不是以日期行结束");

                        // 最后一行的Y坐标
                        lastRowY = curTv.bounds().top - 0;
                        console.log(Constant.UI_SDK_VERSION + "doScanBillsForEachScreen-当前: " + curText + ", Y坐标: " + lastRowY);
                    }

                } else {
                    // 不是有效的行数据, 需要往下滑
                    console.log(Constant.UI_SDK_VERSION + "doScanBillsForEachScreen-当前: " + startIndex + ", 不是有效数据行==只展示部分行");

                    // 最后一行的Y坐标
                    lastRowY = curTv.bounds().top - 0;
                    console.log(Constant.UI_SDK_VERSION + "doScanBillsForEachScreen-当前: " + curText + ", Y坐标: " + lastRowY);
                }
            } else {
                // 不是有效的行数据, 需要继续遍历
                console.log(Constant.UI_SDK_VERSION + "doScanBillsForEachScreen-当前: " + curText + ", 不是有效数据行==不是Sent与Receive开头");
            }
            startIndex += 1;
        }

        // 释放内存
        textList.length = 0;
        textList = null;

        console.log(Constant.UI_SDK_VERSION + "doScanBillsForEachScreen-当屏数据已处理完: " + pageSize + ",  到底: " + hadSwipeToBottom + ", lastRowY: " + lastRowY);

        // 只对首屏的数据做标识, 因为如果连首屏的数据都变了, 其它数据肯定变了
        // if (isFirstScreen) {
        //     // 缓存上一屏数据
        //     doDeepCloneShortBills(lastScreenBillList, curScreenBillList);
        // }

        // 判断是否需要继续滑动
        if (hadSwipeToBottom) {
            console.log(Constant.UI_SDK_VERSION + "doScanBillsForEachScreen-当屏数据已处理完：" + pageSize + " 所有数据已处理完毕=====")
            // 滑动到顶部
            swipe(xPullStart, yPullStart, xPullStart, yPullEnd, 1 * 1000);
            sleep(3 * 1000);
            return true;

        } else {
            console.log(Constant.UI_SDK_VERSION + "doScanBillsForEachScreen-当屏数据已处理完：" + pageSize + " 准备往下滑动=====")
            // 滑动 需要去掉60的加界点, 不然会跟手势冲突
            if ((lastRowY > 0) && (lastRowY < yPullEnd)) {
                swipe(xPullStart, lastRowY, xPullStart, yPullStart, 1000);
            } else {
                swipe(xPullStart, yPullEnd, xPullStart, yPullStart, 1000);
            }
            sleep(3 * 1000);
        }
    }

    console.log(Constant.UI_SDK_VERSION + "doScanBillsForEachScreen-结束")
    console.log(Constant.UI_SDK_VERSION + "doScanBillsForEachScreen-共有效数据：" + mBillIdsCached.length);
    for (let i = 0; i < mBillIdsCached.length; i++) {
        let item = mBillIdsCached[i];
        console.log(Constant.UI_SDK_VERSION + "doScanBillsForEachScreen-id: " + item.detailTransactionID);
    }

    return true;
};

// 查看流水详情页
const doGetBillDetails = (shortBillItem) => {
    // 详情内容
    let billDetail = {};
    billDetail.shortAmount = shortBillItem.shortAmount || "";
    billDetail.shortDate = shortBillItem.shortDate || "";
    billDetail.shortType = shortBillItem.shortType || "";

    // 读取当前列表
    let textList = className("TextView").find();
    for (let index = 0; index < textList.length; index++) {
        let curTv = textList[index];
        let curText = curTv.text() || ""
        // console.log(Constant.UI_SDK_VERSION + "doGetBillDetails-当前: " + index + ", 内容: " + curText)

        // 交易金额
        if (index === 0) {
            billDetail.detailAmount = curText;
            continue;
        }

        // 类型
        if (curText.indexOf(WaveFcfaPageTag.billRecordReceived1) !== -1) {
            billDetail.detailType = curText;
            continue;
        }
        if (curText.indexOf(WaveFcfaPageTag.billRecordSent2) !== -1) {
            billDetail.detailType = curText;
            continue;
        }

        // received amount
        if (curText.indexOf(WaveFcfaPageTag.billRecordReceivedAmount) !== -1) {
            let nextIndex = (index + 1);
            if (nextIndex < textList.length) {
                let nextTv = textList[nextIndex];
                let nextText = nextTv.text() || "";
                billDetail.detailReceivedAmount = nextText;
            }
            continue;
        }

        // fee
        if (curText.indexOf(WaveFcfaPageTag.billRecordFee) !== -1) {
            let nextIndex = (index + 1);
            if (nextIndex < textList.length) {
                let nextTv = textList[nextIndex];
                let nextText = nextTv.text() || "";
                billDetail.detailFee = nextText;
            }
            continue;
        }

        // status
        if (curText.indexOf(WaveFcfaPageTag.billRecordStatus) !== -1) {
            let nextIndex = (index + 1);
            if (nextIndex < textList.length) {
                let nextTv = textList[nextIndex];
                let nextText = nextTv.text() || "";
                billDetail.detailStatus = nextText;
            }
            continue;
        }

        // date & time
        if (curText.indexOf(WaveFcfaPageTag.billRecordDateTime) !== -1) {
            let nextIndex = (index + 1);
            if (nextIndex < textList.length) {
                let nextTv = textList[nextIndex];
                let nextText = nextTv.text() || "";
                billDetail.detailDateTime = nextText;
            }
            continue;
        }

        // new balance
        if (curText.indexOf(WaveFcfaPageTag.billRecordNewBalance) !== -1) {
            let nextIndex = (index + 1);
            if (nextIndex < textList.length) {
                let nextTv = textList[nextIndex];
                let nextText = nextTv.text() || "";
                billDetail.detailNewBalance = nextText;
            }
            continue;
        }

        // transaction ID
        if (curText.indexOf(WaveFcfaPageTag.billRecordTransactionID) !== -1) {
            let nextIndex = (index + 1);
            if (nextIndex < textList.length) {
                let nextTv = textList[nextIndex];
                let nextText = nextTv.text() || "";
                billDetail.detailTransactionID = nextText;
            }
            continue;
        }
    }

    textList.length = 0;
    textList = null;

    // 增加缓存
    let addSuccess = doAddBillRecord(billDetail);
    if (addSuccess) {
        // 在这里上报后台
        // ...
        // console.log(billDetail);
    } else {
        // console.log(Constant.UI_SDK_VERSION + "doGetBillDetails-交易id已存在, 不作处理" + billDetail.detailTransactionID);
    }

    sleep(500);
    back();
    return billDetail.detailTransactionID;
};

// ==================== 流水操作-end ====================

////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
// ==================== 放款操作-start ====================

// 点击 放款 Send
const doSendMoney = () => {
    // 滑动点坐标
    let xPullStart = (device.width * 0.8);
    let yPullStart = 280;
    let yPullEnd = (device.height - 60);

    // 滑动到顶部
    swipe(xPullStart, yPullStart, xPullStart, yPullEnd, 1000);
    sleep(2 * 1000);

    // 下拉刷新下
    swipe(xPullStart, 400, xPullStart, 800, 1000);
    sleep(3 * 1000);

    // 先点击 Send
    click(WaveFcfaPageTag.homePageTag2);
    sleep(1000);

    // 循环判断是否为 Send Money 页
    let isOpenSendMoneyPage = WaveFcfaPageUtil.doOpenSendMoneyPage();
    if (isOpenSendMoneyPage) {
        // 点击 Enter a new number
        click(WaveFcfaPageTag.sendMoneyPageTag2);
        sleep(1000);

        // 是否为账号输入页
        let isOpenSendMoneyAccountInputPage = WaveFcfaPageUtil.doOpenSendMoneyAccountInputPage();
        if (isOpenSendMoneyAccountInputPage) {
            // 是否已输入 full name 与 mobile
            let isInputFullName = false;
            let isInputMobile = false;

            // 输入 full name
            let fullNameEdit = textContains(WaveFcfaPageTag.sendMoneyAccountInputPageTag1).findOnce();
            if (fullNameEdit != null) {
                fullNameEdit.setText(Constant.TARGET_WAVE_FCFA_NAME);
                isInputFullName = true;

                let mobileEdit = textContains(WaveFcfaPageTag.sendMoneyAccountInputPageTag2).findOnce();
                if (mobileEdit != null) {
                    mobileEdit.setText(Constant.TARGET_WAVE_FCFA_MOBILE);
                    isInputMobile = true;
                }
            }

            // 判断是否输入完成
            if (isInputFullName && isInputMobile) {
                let nextButton = textContains(WaveFcfaPageTag.sendMoneyAccountInputPageTag6).findOnce();
                nextButton.click();
                sleep(1000);

                // 判断是否在输入金额页
                let isOpenSendMoneyAmountInputPage = WaveFcfaPageUtil.doOpenSendMoneyAmountInputPage();
                if (isOpenSendMoneyAmountInputPage) {
                    // 找到 receive amount edit
                    let receiveAmountEdit = textContains(WaveFcfaPageTag.sendMoneyAmountInputPageTag2).findOnce();
                    // 是否已输入 receive amount
                    let isInputReceiveAmount = false;

                    if (receiveAmountEdit != null) {
                        receiveAmountEdit.click();
                        sleep(1000);
                        receiveAmountEdit.setText(Constant.TARGET_WAVE_FCFA_AMOUNT);
                        sleep(1000);
                        click(630, 1280);
                        isInputReceiveAmount = true;
                        sleep(1000);
                    }

                    // 判断 receive amount 是否输入完成
                    if (isInputReceiveAmount) {
                        let sendButton = id(WaveFcfaPageTag.sendMoneyAmountInputSendButtonId).findOnce();
                        sendButton.click();
                        sleep(1000);

                        // 查询下是否是确认交易页
                        let isOpenSendMoneyAmountInputConfirmPage = WaveFcfaPageUtil.doOpenSendMoneyAmountInputConfirmPage();
                        if (isOpenSendMoneyAmountInputConfirmPage) {
                            let confirmButton = WaveFcfaPageUtil.findSendMoneyAmountInputConfirmButton();
                            if (confirmButton != null) {
                                confirmButton.click();
                                sleep(1000);

                                // 判断是否失败
                                let isSendMoneyFail = WaveFcfaPageUtil.isSendMoneyFailPage();
                                if (isSendMoneyFail) {
                                    // 返回首页
                                    WaveFcfaPageUtil.doToHomePageFromSendMoneyFailPage();
                                    return false;
                                }

                                // 判断是否成功
                                let isSendMoneySuccess = WaveFcfaPageUtil.isSendMoneySuccessPage();
                                // 返回转账成功：正常来说都会成功，可以申请撤回
                                return isSendMoneySuccess;
                            }
                        }
                    }
                }
            }
        }
    }

    return false;
};

// ==================== 放款操作-end ====================

module.exports = {
    doTestArray: doTestArray,
    doLogin: doLogin,
    doScanBillsForEachScreen: doScanBillsForEachScreen,
    doSendMoney: doSendMoney,
    doGetBillDetails: doGetBillDetails,
};